package com.jarchie.htgl.fragment;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jarchie.htgl.R;
import com.jarchie.htgl.adapter.ContractManAdapter;
import com.jarchie.htgl.base.BaseApp;
import com.jarchie.htgl.base.BaseFragment;
import com.jarchie.htgl.bean.UserLogin;
import com.jarchie.htgl.fragment.contract.ConApproveFragment;
import com.jarchie.htgl.fragment.contract.ConInvalidApplyFragment;
import com.jarchie.htgl.fragment.contract.ConInvalidAuditFragment;
import com.jarchie.htgl.fragment.contract.ConRegisterFragment;
import com.jarchie.htgl.fragment.contract.ConSealFragment;
import com.jarchie.htgl.fragment.contract.ContractInvalidFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jarchie on 2017\12\12.
 * 合同管理的Fragment
 */

public class ContractManFragment extends BaseFragment implements ViewPager.OnPageChangeListener {
    private LinearLayout exceptionLayout, contentLayout;
    private TextView tipsMessage;
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private LinearLayout leftLayout, rightLayout;
    private TextView mTitle;
    private List<String> mTitles = new ArrayList<>();
    private List<Fragment> mFragments = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_contract_manage;
    }

    @Override
    protected void initView(View rootView) {
        contentLayout = obtainView(R.id.content_layout);
        mTabLayout = obtainView(R.id.mTabLayout);
        mViewPager = obtainView(R.id.mViewPager);
        mTitle = obtainView(R.id.title_text);
        leftLayout = obtainView(R.id.left_layout);
        rightLayout = obtainView(R.id.right_layout);
        exceptionLayout = obtainView(R.id.exception_layout);
        tipsMessage = obtainView(R.id.tv_exception);
    }

    @Override
    protected void setListener() {
        mViewPager.addOnPageChangeListener(this); //ViewPager的滑动监听
    }

    @Override
    protected void initData() {
        handleRolePermissions(); //处理角色权限问题
        if (mFragments.size() == 0) {
            contentLayout.setVisibility(View.GONE);
            exceptionLayout.setVisibility(View.VISIBLE);
            tipsMessage.setText("对不起，该账户下未找到相关页面");
        } else if (mFragments.size() == 1) {
            mTabLayout.setVisibility(View.GONE);
        } else if (mFragments.size() > 4) {
            mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        } else {
            mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        }
        if (mTitles.size() > 0) { //初始化顶部标题栏
            if (mTitles.get(0).equals(getString(R.string.contract_approve))) {
                mTitle.setText(getString(R.string.contract_approve));
            } else if (mTitles.get(0).equals(getString(R.string.contract_seal))) {
                mTitle.setText(getString(R.string.contract_seal));
            } else if (mTitles.get(0).equals(getString(R.string.contract_invalid_apply))) {
                mTitle.setText(getString(R.string.contract_invalid_apply));
            } else if (mTitles.get(0).equals(getString(R.string.contract_invalid_audit))) {
                mTitle.setText(getString(R.string.contract_invalid_audit));
            } else if (mTitles.get(0).equals(getString(R.string.contract_register))) {
                mTitle.setText(getString(R.string.contract_register));
            } else if (mTitles.get(0).equals(getString(R.string.contract_invalid))) {
                mTitle.setText(getString(R.string.contract_invalid));
            }
        }
        if (mFragments.size() > 0) {
            mViewPager.setOffscreenPageLimit(mFragments.size()); //预加载
            //设置适配器
            mViewPager.setAdapter(new ContractManAdapter(getChildFragmentManager(), mTitles, mFragments));
            //绑定ViewPager
            mTabLayout.setupWithViewPager(mViewPager);
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

    @Override
    public void onPageSelected(int position) {
        if (mTitles.get(position).equals(getString(R.string.contract_register))) {
            setTitleBar(mTitles.get(position), View.VISIBLE);
        } else {
            setTitleBar(mTitles.get(position), View.INVISIBLE);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {}

    //处理合同管理模块的权限
    private void handleRolePermissions() {
        List<UserLogin.INFOBean.MyjsqxbBean> mList = BaseApp.getInstance().getUserLogin().getINFO().get(0).getMyjsqxb();
        for (int i = 0; i < mList.size(); i++) {
            switch (mList.get(i).getCddm()) {
                case "1005": //合同审批
                    mTitles.add(getString(R.string.contract_approve));
                    mFragments.add(new ConApproveFragment());
                    break;
                case "1010": //合同盖章
                    mTitles.add(getString(R.string.contract_seal));
                    mFragments.add(new ConSealFragment());
                    break;
                case "1015": //作废申请
                    mTitles.add(getString(R.string.contract_invalid_apply));
                    mFragments.add(new ConInvalidApplyFragment());
                    break;
                case "1020": //作废审核
                    mTitles.add(getString(R.string.contract_invalid_audit));
                    mFragments.add(new ConInvalidAuditFragment());
                    break;
                case "1001": //合同登记
                    mTitles.add(getString(R.string.contract_register));
                    mFragments.add(new ConRegisterFragment());
                    break;
                case "1025": //合同作废
                    mTitles.add(getString(R.string.contract_invalid));
                    mFragments.add(new ContractInvalidFragment());
                    break;
            }
        }
    }

    //设置标题栏的属性
    private void setTitleBar(String title, int visiblity) {
        mTitle.setText(title);
        leftLayout.setVisibility(visiblity);
        rightLayout.setVisibility(visiblity);
    }

}
